﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
using System.Linq;
using System.IO;

namespace HttpBot
{
    public class HttpHelper
    {
        public static string Post(string url, string cookie, DicParam pairs)
        {
            HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;
            //req.Proxy = new WebProxy("http://localhost:8888");
            req.Proxy = new WebProxy();
            req.Method = "POST";
            req.Accept = "image/gif, image/jpeg, image/pjpeg, application/x-ms-application, application/xaml+xml, application/x-ms-xbap, */*";
            req.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729)";
            req.Referer = "http://9.80.35.20/picp/SingleInquireAction.do?method=siOpen";
            req.ContentType = "application/x-www-form-urlencoded";
            //CookieContainer co = new CookieContainer();
            //co.SetCookies(new Uri(url), cookie);
            req.CookieContainer = new CookieContainer(); 
            foreach (var item in cookie.Split(';'))
            {
                var x = item.Split('=');
                req.CookieContainer.Add(new Uri(url), new Cookie(x[0], x[1], "/"));
            }

            //using (var rs = req.GetRequestStream())
            //{ 
            //    string arg = string.Join("&", pairs.Params.Select(x =>x.Key + "=" + x.Value));
            //    var buf = Encoding.GetEncoding("GBK").GetBytes(arg);
            //    rs.Write(buf, 0, buf.Length);
            //}

            var res = req.GetResponse();
            using (StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.GetEncoding("GBK")))
            {
                return sr.ReadToEnd();
            }
        }

        /// <summary>
        /// 下载图片
        /// </summary>
        /// <param name="url">地址</param>
        /// <param name="preUrl">refer地址(来源的上一个页面)</param>
        /// <param name="path">图片保存地址</param>
        /// <param name="cookie">cookie</param>
        public static void Download(string url,string preUrl, string path,string cookie)
        {
            HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;
            //req.Proxy = new WebProxy("http://localhost:8888");
            req.Proxy = new WebProxy();
            req.Method = "GET";
            req.Accept = "image/gif, image/jpeg, image/pjpeg, application/x-ms-application, application/xaml+xml, application/x-ms-xbap, */*";
            req.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729)";
            req.Referer = preUrl;
            req.ContentType = "application/x-www-form-urlencoded";
            req.CookieContainer = new CookieContainer();

            foreach (var item in cookie.Split(';'))
            {
                var x = item.Split('=');
                req.CookieContainer.Add(new Uri(url), new Cookie(x[0], x[1], "/"));
            }

            var res = req.GetResponse();
            using (var sm = res.GetResponseStream())
            {
                using (FileStream fs = new FileStream(path, FileMode.Create))
                {
                    while (true)
                    {
                        var buffer = new byte[512];
                        int count = sm.Read(buffer, 0, buffer.Length);
                        if (count <= 0)
                        {
                            return;
                        } 
                        buffer = buffer.Take(count).ToArray(); 
                        fs.Write(buffer, 0, buffer.Length);
                    }
                }
            }
        }
    }
}
